<?php

/*
 * Descripción: Carga toda la informacion de las sugerencias
 */
//Variables del jqgrid
require_once '../funciones/funciones.php';   //Se hace la inclusion para que no genere conflictos con el XML
require_once '../includes/funcionesGrilla.php';
$page = $_GET['page']; // get the requested page
$limit = $_GET['rows']; // get how many rows we want to have into the grid
$sidx = $_GET['sidx']; // get index row - i.e. user click to sort
$sord = $_GET['sord']; // get the direction
if (!$sidx)
    $sidx = 1;
$wh = "";
$searchOn = Strip($_REQUEST['_search']);
if ($searchOn == 'true') {
    $searchstr = Strip($_REQUEST['filters']);
    $jsona = json_decode($searchstr, true);
    $wh = " AND " . getStringForGroup($jsona);
}
//exit($wh);
$con = new MySQL();
$sql = "select COUNT(suger.sugerId) as count from sugerencias suger inner join subtemas subt on subt.subtemaId = suger.subtemaId inner join temas tem on tem.temaId = subt.temaId  where 1= 1 " . $wh;
$result = $con->consulta($sql);
$count = mysql_result($result, 0,"count");
$total_pages = ($count > 0)?ceil($count / $limit):0;
if ($page > $total_pages) $page = $total_pages;
$start = $limit * $page - $limit; // do not put $limit*($page - 1)
$start = ($start > 0) ? $start : 0;
$sql = "select suger.sugerId,suger.sugerTipo,suger.sugerencia,subt.subtemaNombre,tem.temaNombre,suger.sugerDescripcion from sugerencias suger inner join subtemas subt on subt.subtemaId = suger.subtemaId inner join temas tem on tem.temaId = subt.temaId where 1= 1" . $wh . " ORDER BY $sidx $sord LIMIT $start , $limit";
$result = $con->consulta($sql);
//exit($sql);
        $responce->page = $page;
        $responce->total = $total_pages;
        $responce->records = $count;
        $i = 0;
        while($value = $con->unaFila($result)){
            $responce->rows[$i]['id'] = $value['sugerId'];
            $responce->rows[$i]['cell'] = array('', $value['temaNombre'],$value['subtemaNombre'], $value['sugerDescripcion'],$value['sugerTipo'],htmlspecialchars($value['sugerencia']));
            $i++;
        }
        echo json_encode($responce);


//exit("salio");

//
//
//
//if (stristr($_SERVER["HTTP_ACCEPT"], "application/xhtml+xml")) {
//    header("Content-type: application/xhtml+xml;charset=utf-8");
//} else {
//    header("Content-type: text/xml;charset=utf-8");
//}
//$et = ">";
//echo "<?xml version='1.0' encoding='utf-8'?$et\n";
//echo "<rows>";
//
//$result = $con->consulta($sql);
//$row = mysql_fetch_array($result, MYSQL_ASSOC);
//$count = $row['count'];
//if ($page > $total_pages)
//    $page = $total_pages;
//$start = $limit * $page - $limit; // do not put $limit*($page - 1)
//$start = ($start < 0) ? 0 : $start;
//echo "<page>" . $page . "</page>";
//echo "<total>" . $total_pages . "</total>";
//echo "<records>" . $count . "</records>"; // be sure to put text data in CDATA$sql
////Consulta cantidad
//while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
//    echo "<row id='" . $row['sugerId'] . "'>";
//    echo "<cell></cell>";
//    echo "<cell>" . $row['temaNombre'] . "</cell>";
//    echo "<cell>" . $row['subtemaNombre'] . "</cell>";
//    echo "<cell>" . $row['sugerDescripcion'] . "</cell>";
//    echo "<cell>" . $row['sugerTipo'] . "</cell>";
//    echo "<cell>" . htmlspecialchars($row['sugerencia']) . "</cell>";
//    echo "</row>";
//}
//echo "</rows>";
?>
